/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import DAO.CauHoiDAO;
import DTO.CauHoiDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author THANHLNSE60620
 */
public class CauHoiServlet extends HttpServlet {

    private static String homepage = "indexCauHoi.html";
    private static String invalid = "invalid.html";
    private static String searchPage = "searchCauHoi.jsp";
    private static String updatePage = "updateCauHoi.jsp";

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String button = request.getParameter("btAction");
            if (button.equals("Send")) {//start Insert
                String cauhoi = request.getParameter("txtCauHoi");
                int customerCode = 10002;
                int maNhanVien = 1;
                String noiDungTraLoi = "N/A";
                DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
                Date date = new Date();// lấy ngày giờ hệ thống
                String ngayGui = dateFormat.format(date);
                CauHoiDTO dto = new CauHoiDTO(0, cauhoi.trim(), customerCode, maNhanVien, noiDungTraLoi, ngayGui);
                boolean check = CauHoiDAO.insertCauHoi(dto);
                String url = invalid;
                if (check) {
                    url = homepage;
                }
                response.sendRedirect(url);
            }//end insert
            else if (button.equals("Search")) {//start Search
                String date = request.getParameter("txtDate");
                CauHoiDTO[] a = CauHoiDAO.searchCauHoi(date);

                request.setAttribute("INFOR", a);
                RequestDispatcher rd = request.getRequestDispatcher(searchPage);
                rd.forward(request, response);
            }//end Search
            else if (button.equals("Delete")) {//start delete
                String id = request.getParameter("id");//tren url
                String searchValue = request.getParameter("searchQueryString");
                boolean result = CauHoiDAO.deleteCauHoi(id);
                String url = homepage;

                if (result) {
                    url = "CauHoiServlet?btAction=Search&txtDate= " + searchValue;

                }
                response.sendRedirect(url);
            } else if (button.equals("Edit")) {//start Edit
                String id = request.getParameter("txtId");
               
                System.out.println("ID: " + id);
                CauHoiDTO[] a = CauHoiDAO.searchCauHoiById(id);
                for (CauHoiDTO b : a) {
                 
                }
                request.setAttribute("INFOR", a);
                RequestDispatcher rd = request.getRequestDispatcher(updatePage);
                rd.forward(request, response);

            }//end Edit
            else if (button.equals("Save")) {//start Update cau hoi
                String id = request.getParameter("txtId");
                String maNV = "1";
                String noidung = request.getParameter("txtTraLoi");
                CauHoiDAO.updatCauHoi(maNV, noidung.trim(), id);
                response.sendRedirect(searchPage);
            }//end Update cau hoi
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
